package com.lijing.mock.model.bo;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.springframework.data.annotation.Id;

import java.io.Serializable;
import java.util.List;

/**
 * url 随机配置信息(根据UrlMockBO配置推导而出)
 * @author by lijing
 * @date 2019/12/3 0003 15:35
 */
@Getter
@Setter
@ToString
@NoArgsConstructor
public class UrlRandomBO implements Serializable {
    @Id
    private String id;
    /** url */
    private String url;
    /** 权重域 */
    private List<MockRegionBO> weightRegion;
    /** 总权重 */
    private Integer totalWeight;


    /**
     * 根据随机值获取当前值所对应的url mock配置数据ID
     * @param randomValue 随机值
     * @return mock数据类型
     */
    public MockRegionBO getUrlId(Integer randomValue){
        if(randomValue<0){
            //随机数据异常(理论不存在)
            return null;
        }
        for (MockRegionBO mockRegion : weightRegion) {
            if (mockRegion.getLeftValue() <= randomValue &&
                    mockRegion.getRightValue() > randomValue) {
                return mockRegion;
            }
        }
        //数据越界(理论不存在)
        return null;
    }

}
